<?php
include 'sysco.function.php';
error_log('start');
$con = mysql_connect($HOST, $USER, $PASS);
mysql_select_db($DB);
error_log('connected');

$totTax = getTotalTaxes($MERCHANT); 
$totOrdersWithTax = getTotalSalesWithTax($MERCHANT);

// reduce free and cashin orders
$reductions = getTotalSalesWithTax($MERCHANT, 'cashin'); // + getTotalSalesWithTax($MERCHANT, 'free') + getTotalSalesWithTax($MERCHANT, 'comp');
$totOrdersWithTax = $totOrdersWithTax - $reductions;
$taxRatio = ($totTax/$totOrdersWithTax);
error_log('tot tax = '.$totTax.' = tot amount '.$totOrdersWithTax.' = '. $taxRatio); 

$totOrders = getAmountNoTax($totOrdersWithTax, $taxRatio);
$t = getTotalSalesWithTax($MERCHANT, 'online');
// echo $t." - online \n";
$oloOrders = getAmountNoTax($t, $taxRatio);
$t = getTotalSalesWithTax($MERCHANT, 'magcard');
// echo $t." - magcard \n";
$ccOrders = getAmountNoTax($t, $taxRatio);
$t = getTotalSalesWithTax($MERCHANT, 'paypal');
// echo $t." - paypal \n";
$ppOrders = getAmountNoTax($t, $taxRatio);
$t = getTotalSalesWithTax($MERCHANT, 'cash');
// echo $t." - cash /n";
$cashOrders = getAmountNoTax($t, $taxRatio);
$t = getTotalSalesWithTax($MERCHANT, 'cheque');
// echo $t." - cheque /n";
$chequeOrders = getAmountNoTax($t, $taxRatio);

// echo $totOrders.' '.$ccOrders.' '.$cashOrders.' '.$chequeOrders."\n";

$_SESSION['from'] = '2012-12-01 03:00:00';
$_SESSION['to'] = '2013-01-01 02:59:00';


$prevTotTax = getTotalTaxes($MERCHANT); 
$prevTotOrdersWithTax = getTotalSalesWithTax($MERCHANT);

// reduce free and cashin orders
$reductions = getTotalSalesWithTax($MERCHANT, 'cashin'); // + getTotalSalesWithTax($MERCHANT, 'free') + getTotalSalesWithTax($MERCHANT, 'comp');
$prevTotOrdersWithTax = $prevTotOrdersWithTax - $reductions;
$prevTaxRatio = ($prevTotTax/$prevTotOrdersWithTax);
error_log('tot tax = '.$prevTotTax.' = tot amount '.$prevTotOrdersWithTax.' = '. $prevTaxRatio); 

$prevTotOrders = getAmountNoTax($prevTotOrdersWithTax, $prevTaxRatio);
$prevOloOrders = getAmountNoTax(getTotalSalesWithTax($MERCHANT, 'online'), $prevTaxRatio);
$prevCcOrders = getAmountNoTax(getTotalSalesWithTax($MERCHANT, 'magcard'), $prevTaxRatio);
$prevPpOrders = getAmountNoTax(getTotalSalesWithTax($MERCHANT, 'paypal'), $prevTaxRatio);
$prevChequeOrders = getAmountNoTax(getTotalSalesWithTax($MERCHANT, 'cheque'), $prevTaxRatio);

$fp = fopen('reports/sysco_snapshot_tax_'.$_SESSION['from'].'-'.$MERCHANT.'.csv', 'w');

$tmp = array('tot sales', 'olo', 'cc', 'pp', 'cash', 'cheque', 'tot sale %','olo %','cc %', 'pp %', 'chq %');
fputcsv($fp, $tmp);

$tot_prc = $prevTotOrders > 0 ? (($totOrders-$prevTotOrders)/$prevTotOrders)*100 : 100;
$olo_prc = $prevOloOrders > 0 ? (($oloOrders-$prevOloOrders)/$prevOloOrders)*100 : 100;
$cc_prc = $prevCcOrders > 0 ? ((($ccOrders+$oloOrders)-($prevCcOrders+$prevOloOrders))/($prevCcOrders+$prevOloOrders))*100 : 100;
$pp_prc = $prevPpOrders > 0 ? (($ppOrders-$prevPpOrders)/$prevPpOrders)*100 : 100;
$chq_prc = $prevChequeOrders > 0 ? (($chequeOrders-$prevChequeOrders)/$prevChequeOrders)*100 : 100;

$tmp = array($totOrders, $oloOrders, ($ccOrders+$oloOrders), $ppOrders, $cashOrders, $chequeOrders, $tot_prc, $olo_prc, $cc_prc, $pp_prc, $chq_prc);
fputcsv($fp, $tmp);

$tmp = array('', '');
fputcsv($fp, $tmp);
$tmp = array('cash %', 'PP %', 'cc %', 'cheque %');
fputcsv($fp, $tmp);
$tmp = array((($cashOrders/$totOrders)*100), (($ppOrders/$totOrders)*100), ((($ccOrders+$oloOrders)/$totOrders)*100), (($chequeOrders/$totOrders)*100));
fputcsv($fp, $tmp);

$tmp = array('ONLINE %', 'OFFLINE %');
fputcsv($fp, $tmp);
$tmp = array((($oloOrders/$totOrders)*100), ((($totOrders-$oloOrders)/$totOrders)*100));
fputcsv($fp, $tmp);
$tmp = array('-----', '---------');
fputcsv($fp, $tmp);



$amounts = array();
$_SESSION['from'] = '2013-01-26 03:00:00';
$_SESSION['to'] = '2013-03-01 02:59:00';

$totOrders = getTotalSalesWithTax($MERCHANT);
$totOrderList = $_SESSION['to_orders'];
foreach ($totOrderList as $totOrder) {
  $index = date('m/d', strtotime($totOrder['DATENEW']));
  $amounts[$index] = isset($amounts[$index]) ? $amounts[$index]+ getAmountNoTax($totOrder['TOT_AMOUNT'], $taxRatio) : getAmountNoTax($totOrder['TOT_AMOUNT'], $taxRatio);
}

foreach ($amounts as $key => $amount) {
  fputcsv($fp, array($key, $amount));
}

fclose($fp);
?>